package com.lzp.common.batch;

import com.lzp.entity.WechatBill;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DefaultFieldSetFactory;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.zip.ZipFile;

/**
 * @author lzp
 * @version 1.0.0
 * @date 2022/5/19 20:22:07
 * @apiNote 将压缩包根据密码解压出来,放到指定目录里,并将数据读入内存以便进行后续处理
 */
@Component
@DependsOn("weChatFieldSetMapper")
public class ZipItemReader extends FlatFileItemReader<WechatBill> {
    @Resource
    WeChatFieldSetMapper weChatFieldSetMapper;
    org.springframework.core.io.Resource resource;
    ZipFile zipFile;

    public ZipFile getZipFile() {
        return zipFile;
    }

    public void setZipFile(ZipFile zipFile) {
        this.zipFile = zipFile;
    }

    public void setResourceChild(org.springframework.core.io.Resource resource) {
        this.resource = resource;
    }
    @Override
    protected WechatBill doRead() throws Exception {

        this.setLinesToSkip(16);
        if (resource==null){
            throw new IllegalArgumentException("需要操作的资源为空");
        }
        setResource(this.resource);
        DefaultLineMapper<WechatBill> defaultLineMapper = new DefaultLineMapper<>();
        DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer();
        delimitedLineTokenizer.setFieldSetFactory(new DefaultFieldSetFactory());
        delimitedLineTokenizer.setDelimiter(",");
        defaultLineMapper.setLineTokenizer(delimitedLineTokenizer);
        defaultLineMapper.setFieldSetMapper(weChatFieldSetMapper);
        setLineMapper(defaultLineMapper);
        return super.doRead();
    }
}
